﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;

namespace Cay
{
    class DataProvider
    {
        string conString;
        SqlConnection con;
        SqlCommand com;
        public DataProvider()
        {
            try
            {
                conString = ConfigurationManager.ConnectionStrings["EbookCon"].ConnectionString;
                con = new SqlConnection(conString);
                con.Open();

            }
            catch (Exception ex) { }

        }
        public SqlDataReader executeReader(string query, string paraName, string paraValue)
        {
            com = new SqlCommand(query, con);
            com.Parameters.AddWithValue(paraName, paraValue);
            return com.ExecuteReader();

        }
        public SqlDataReader executeReader(string query, string paraName, int paraValue)
        {
            com = new SqlCommand(query, con);
            com.Parameters.AddWithValue(paraName, paraValue);
            return com.ExecuteReader();

        }
        public SqlDataReader executeReader(string query)
        {
            com = new SqlCommand(query, con);
            return com.ExecuteReader();

        }
        public object executeScalar(string query, string paraName, int paraValue)
        {
            com = new SqlCommand(query, con);
            com.Parameters.AddWithValue(paraName, paraValue);
            return com.ExecuteScalar();
        }
        public void executeNonQuery(string query, List<string> paraName, List<object> paraValue)
        {
            com = new SqlCommand(query, con);
            for (int i = 0; i < paraName.Count(); i++)
            {
                com.Parameters.AddWithValue(paraName[i], paraValue[i]);
            }
            com.ExecuteNonQuery();
                
            
        }
        public void close()
        {
            com.Dispose();
            con.Close();
            con.Dispose();
        }
        public string test()
        {
            try
            {
                conString = ConfigurationManager.ConnectionStrings["EbookCon"].ConnectionString;
                con = new SqlConnection(conString);
                con.Open();
                return "connect";

            }
            catch (Exception ex) { return ex.ToString(); }
        }
    }
}
